package com.example.wechathook.hook;

import static de.robv.android.xposed.XposedBridge.log;
import static de.robv.android.xposed.XposedHelpers.callStaticMethod;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import static de.robv.android.xposed.XposedHelpers.findClass;
import static de.robv.android.xposed.XposedHelpers.getAdditionalInstanceField;
import static de.robv.android.xposed.XposedHelpers.getObjectField;
import static de.robv.android.xposed.XposedHelpers.removeAdditionalInstanceField;
import static de.robv.android.xposed.XposedHelpers.setAdditionalInstanceField;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

public class LauncherUIHook extends BaseHook {

	public static final String TAG = "launcherUIMod";
	private ClassLoader classLoader;
	private Context context;
	
	@Override
	public void hook(LoadPackageParam lpp) {
		classLoader = lpp.classLoader;
		findAndHookMethod("com.tencent.mm.ui.LauncherUI", classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
			@Override
			protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
				Activity launcherUIInstance = (Activity) param.thisObject;
				context = launcherUIInstance.getApplicationContext();
				LauncherUI launcherUIMod = new LauncherUI(launcherUIInstance);
				setAdditionalInstanceField(launcherUIInstance, TAG, launcherUIMod);
			}
		});

		findAndHookMethod("com.tencent.mm.ui.LauncherUI", classLoader, "bee", new XC_MethodHook() {
			@Override
			protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
				LauncherUI launcherUIMod = (LauncherUI) getAdditionalInstanceField(param.thisObject, TAG);
				launcherUIMod.isMainTabCreated = (boolean) getObjectField(param.thisObject, "kzg");
			}

			@Override
			protected void afterHookedMethod(MethodHookParam param) throws Throwable {
				LauncherUI launcherUIMod = (LauncherUI) getAdditionalInstanceField(param.thisObject, TAG);
				if (!launcherUIMod.isMainTabCreated) {
					Class obj = findClass("com.tencent.mm.model.ah", classLoader);
					if ((boolean) callStaticMethod(obj, "qW")) {
						launcherUIMod.moveTabsToTop();
					} else {
						log("mmcore has not ready, finish LauncherUI hook");
					}
				}
			}
		});

		findAndHookMethod("com.tencent.mm.ui.LauncherUI", lpp.classLoader, "onDestroy", new XC_MethodHook() {
			@Override
			protected void afterHookedMethod(MethodHookParam param) throws Throwable {
				LauncherUI launcherUIMod = (LauncherUI) getAdditionalInstanceField(param.thisObject, TAG);
				launcherUIMod.onDestroy();
				removeAdditionalInstanceField(param.thisObject, TAG);
			}
		});

		findAndHookMethod("com.tencent.mm.ui.LauncherUIBottomTabView", classLoader, "a", int.class, ViewGroup.class,
				new XC_MethodHook() {
					@Override
					protected void afterHookedMethod(MethodHookParam param) throws Throwable {
						Object result = param.getResult();
						TextView tabBottom = (TextView) getObjectField(result,"kBj");
						tabBottom.setVisibility(View.GONE);
						View bg = (View) getObjectField(result, "kBh");
						bg.setBackgroundColor(context.getResources().getColor(2131689824));
					}
				});
	}

	class LauncherUI {
		private boolean isMainTabCreated;
		private Activity mLauncherUI;

		public LauncherUI(Activity launcherUI) {
			mLauncherUI = launcherUI;
		}

		private void moveTabsToTop() {
			ViewGroup customViewPager = (ViewGroup) getObjectField(mLauncherUI, "kzq");// CustomerViewPager
			View tabView = (View) getObjectField(mLauncherUI, "kzp");
			LinearLayout parent = (LinearLayout) customViewPager.getParent();
			parent.removeView(tabView);
			parent.addView(tabView, 0);
		}

		private void onDestroy() {
			mLauncherUI = null;
		}

	}
}
